/*
 * WLabel.java
 *
 * Created on 18 November 2007, 22:15
 *
 */

package net.ubinow.mflex.widgets;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import net.ubinow.mflex.baseui.AbsWidget;
import net.ubinow.mflex.baseui.WFont;
import net.ubinow.mflex.data.IDataItem;
import net.ubinow.mflex.data.SimpleDataItem;
import net.ubinow.mflex.util.Constants;

/**
 * This class implements a user interface label class. This is a
 * passive object in that it does not respond to any events (most basic type of 
 * AbsWidget, will either display a image, bg, or text, items[0]).
 * 
 * Used to display either text or a image 
 * @author Josh
 */
public class WLabel extends AbsWidget {        
    
    private int _itemPaddingX = Constants.AD_TEXT_PADDING_X;
    private int _itemPaddingY = Constants.AD_TEXT_PADDING_Y;
    
    private WFont _font = null;
    
    ///
    // text positioning
    ///
    private int _alignment = 0;
    private int _itemX = -1;
    private int _itemY = -1;
    
    private Image _icon = null;        
    
    public WLabel( String id, WFont font, String text, int textAlignment ) {
        _id = id;
        _font = font;
        
        setItems( 
                new IDataItem[]{ new SimpleDataItem( text ) } 
            ); 
        
        //_alignment = textAlignment;
        _width = _font.stringWidth( text );
        _height = _font.getHeight();
     
        init();
    }
    
    public WLabel( String id, Image icon, int imageAlignment ){
        _id = id;
        setBG( icon );
        
        //_alignment = imageAlignment;
        
        _width = icon.getWidth();
        _height = icon.getHeight();
        
        init();
    }
    
    private void init(){
        //setSelectable( false );
        
        //_alignment = Constants.ALIGN_CENTRE_V | Constants.ALIGN_CENTRE_H;
        _itemX = -1;
        _itemY = -1;
        
    }
    
    public void setLabel( WFont font, String text ){
        _font = font;               
        
        setBG( null );
        
        setItems( 
                new IDataItem[]{ new SimpleDataItem( text ) } 
        );    
        
        _itemX = -1;
        _itemY = -1;
    }
    
    public void setIcon( Image icon ){
        setItems( null );
        
        setBG( icon );
        
        _itemX = -1;
        _itemY = -1;
    }
    
    public void setX( int x ){
        _itemX= -1;
        super.setX( x );
    }

    //<editor-fold defaultstate="collapsed" desc="AbsWidget methods">
    
    protected void setWidgetType() {
    }

    public void setBG(Image bg) {
        _icon = bg; 
    }

    public void setBG(int colour) {
        // currently not implemented
    }

    public void cycle() {
        
    }

    public void paint(Graphics g, int offsetX, int offsetY, int layer) {
        if( _itemX == -1 ){
            if( _items != null )
                realignX( _font.stringWidth( getSelectedText() ) ) ;
            else 
                realignX( _icon.getWidth() ) ;
        }
        
        if( _itemY == -1 ){
            if( _items != null )
                realignY( _font.getHeight() ) ;
            else 
                realignY( _icon.getHeight() ) ;
        }
        
        if( _items != null && _items.length > 0 ){
            //System.out.println("I am a text label at: " + (_x + _itemX + offsetX) + ", and " + (_y + _itemY + offsetY) );
            _font.drawString( g, getSelectedText(), _x + _itemX + offsetX, _y + _itemY + offsetY );
        }
        
        if( _icon != null ){            
            //System.out.println("I am a image drawing at: " + (_x + _itemX + offsetX) + ", and " + (_y + _itemY + offsetY) );
            g.drawImage( _icon, _x + _itemX + offsetX, _y + _itemY + offsetY, Constants.GFX_ALIGN_TOP_LEFT );
        }
        
    }
    
    private void realignX( int itemWidth ){
        // work out text position x
        if( (_alignment & Constants.ALIGN_LEFT) == Constants.ALIGN_LEFT ){
            _itemX = _x+_itemPaddingX;
        } else if( (_alignment & Constants.ALIGN_RIGHT) == Constants.ALIGN_RIGHT ){
            _itemX = _x-_itemPaddingX+(_width-itemWidth ); 
        } else if( (_alignment & Constants.ALIGN_CENTRE_H) == Constants.ALIGN_CENTRE_H ){
            _itemX = _x + (_width-itemWidth>>1); 
        }
    }        
    
    private void realignY( int itemHeight ){
        // work out text position x
        if( (_alignment & Constants.ALIGN_TOP) == Constants.ALIGN_TOP ){
            _itemY = _y+_itemPaddingY; 
        } else if( (_alignment & Constants.ALIGN_CENTRE_V) == Constants.ALIGN_CENTRE_V ){
            _itemY = _y + ((_height-itemHeight)>>1); 
        } else if( (_alignment & Constants.ALIGN_BOTTOM) == Constants.ALIGN_BOTTOM ){
            _itemY = _y+_height-_itemPaddingY-itemHeight; 
        }
    }

    public boolean handleKeyPressed(int action, int keyCode) {
        return false; 
    }

    public boolean handleKeyReleased(int action, int keyCode) {
        return false; 
    }
    
    //</editor-fold>
    
}
